package com.example.thread.demo;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * @author zhy
 * @version 1.0.0
 * @ClassName StopExample.java
 * @Description TODO
 * @createTime 2021年01月13日 16:14:00
 */
public class StopExample {

    private static boolean stopRequested;

    @Test
    public void test() throws InterruptedException {
        Thread backgroundThread = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (!stopRequested) {
                    i++;
                }
            }
        });
        backgroundThread.start();

        TimeUnit.SECONDS.sleep(1);
        stopRequested = true;
    }
}
